package gold.gold01;

/**
 * 简单
 */
public class S0301三合一 {
    /**
     * 97, 95
     * 我这里有点问题……考虑到了从前面拿点, 导致栈尾跑到栈头前这么个情况, 所以我的应该是双向队列
     * 题目的是栈, 不需要那些 %stackSize的操作。问题不大
     */
    private int[] threeStack;
    // position是指在各自的小区间的相对位置, 在threeStack中的大位置是需要和stackNum一起得到的
    // 每个Stack有三个参数, 分别为, 起始坐标, 当前坐标(value为空), 栈是否满。
    private int[][] position = new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
    private final int stackSize;

    public S0301三合一(int stackSize) {
        this.threeStack = new int[stackSize * 3];
        this.stackSize = stackSize;
    }

    public void push(int stackNum, int value) {
        if(stackSize == 0 || position[stackNum][2] == 1){
            System.out.println("这个栈已经满了, abaaba。");
            return;
        }
        threeStack[stackNum * stackSize + position[stackNum][1]] = value;
        position[stackNum][1] = (position[stackNum][1] + 1) % stackSize;
        // 只有push的时候会出现满栈, 所以在这里处理一下即可。
        if(position[stackNum][1] == position[stackNum][0])
            position[stackNum][2] = 1;
    }

    public int pop(int stackNum) {
        if((stackSize == 0) || isEmpty(stackNum)) return -1;
        position[stackNum][1] = (position[stackNum][1] + stackSize - 1) % stackSize;
        position[stackNum][2] = 0; // 肯定不是1了, 就不判断了, 直接0;
        return threeStack[stackNum * stackSize + position[stackNum][1]];
    }

    public int peek(int stackNum) {
        if(stackSize == 0 || isEmpty(stackNum)) return -1;
        int temp = pop(stackNum);
        push(stackNum, temp);
        return temp;
    }

    public boolean isEmpty(int stackNum) {
        return (position[stackNum][2] == 0) && (position[stackNum][0] == position[stackNum][1]);
    }

    public static void main(String[] args) {
        S0301三合一 test = new S0301三合一(3);
        test.push(1, 2);
        test.push(1, 3);
        test.push(1, 4);
        test.push(1, 5);
        System.out.println(test.pop(1));
        System.out.println(test.pop(1));
        System.out.println(test.pop(1));
        System.out.println(test.pop(1));
        System.out.println(test.isEmpty(1));
    }
}